home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / Dylan Related / Mindy / Mindy 1.2 - portable sources / interp / module.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  28.8 KB  |  1,140 lines  |  [TEXT/ttxt]

  1. /**********************************************************************\
  2. *
  3. *  Copyright (c) 1994  Carnegie Mellon University
  4. *  All rights reserved.
  5. *  
  6. *  Use and copying of this software and preparation of derivative
  7. *  works based on this software are permitted, including commercial
  8. *  use, provided that the following conditions are observed:
  9. *  
  10. *  1. This copyright notice must be retained in full on any copies
  11. *     and on appropriate parts of any derivative works.
  12. *  2. Documentation (paper or online) accompanying any system that
  13. *     incorporates this software, or any part of it, must acknowledge
  14. *     the contribution of the Gwydion Project at Carnegie Mellon
  15. *     University.
  16. *  
  17. *  This software is made available "as is".  Neither the authors nor
  18. *  Carnegie Mellon University make any warranty about the software,
  19. *  its performance, or its conformity to any specification.
  20. *  
  21. *  Bug reports, questions, comments, and suggestions should be sent by
  22. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  23. *
  24. ***********************************************************************
  25. *
  26. * $Header: module.c,v 1.22 94/12/02 06:33:51 wlott Exp $
  27. *
  28. * This file implements the module system.
  29. *
  30. \**********************************************************************/
  31.  
  32. #include "../compat/std-c.h"
  33.  
  34. #include "mindy.h"
  35. #include "gc.h"
  36. #include "sym.h"
  37. #include "list.h"
  38. #include "bool.h"
  39. #include "str.h"
  40. #include "obj.h"
  41. #include "module.h"
  42. #include "class.h"
  43. #include "type.h"
  44. #include "thread.h"
  45. #include "func.h"
  46. #include "def.h"
  47. #include "load.h"
  48. #include "error.h"
  49. #include "print.h"
  50.  
  51. obj_t obj_Unbound = NULL;
  52. static obj_t obj_UnboundClass = NULL;
  53.  
  54. static boolean InitializingVars = TRUE;
  55.  
  56. struct bucket {
  57.     obj_t symbol;
  58.     void *datum;
  59.     struct bucket *next;
  60. };
  61.  
  62. struct table {
  63.     int entries;
  64.     int threshold;
  65.     int length;
  66.     struct bucket **table;
  67. };
  68.  
  69. struct library {
  70.     obj_t name;
  71.     struct defn *defn;
  72.     struct table *modules;
  73.     boolean loading;
  74.     boolean busy;
  75.     boolean completed;
  76.     struct library *next;
  77. };
  78.  
  79. struct entry {
  80.     obj_t name;
  81.     void *datum;
  82.     boolean exported;
  83.     obj_t origin;
  84. };
  85.  
  86. struct module {
  87.     obj_t name;
  88.     struct library *home;
  89.     struct defn *defn;
  90.     struct table *variables;
  91.     boolean busy;
  92.     boolean completed;
  93.     struct module *next;
  94. };
  95.  
  96. struct var {
  97.     boolean created;
  98.     struct var *next;
  99.     struct variable variable;
  100. };
  101.  
  102. static struct table *LibraryTable = NULL;
  103. static struct library *Libraries = NULL;
  104. static struct module *Modules = NULL;
  105. static struct var *Vars = NULL;
  106.  
  107. static struct library *library_Dylan = NULL;
  108. struct module *module_BuiltinStuff = NULL;
  109.  
  110. static struct module *make_module(obj_t name, struct library *home);
  111. static struct var *make_var(obj_t name, struct module *home, enum var_kind k);
  112.  
  113.  
  114. /* Table manipulation stuff. */
  115.  
  116. static struct table *make_table(void)
  117. {
  118.     struct table *table = (struct table *)malloc(sizeof(struct table));
  119.  
  120.     table->entries = 0;
  121.     table->threshold = 96;
  122.     table->length = 64;
  123.     table->table = (struct bucket **)malloc(sizeof(struct bucket *)*64);
  124.     memset(table->table, 0, sizeof(struct bucket *)*64);
  125.  
  126.     return table;
  127. }
  128.  
  129. static void *table_lookup(struct table *table, obj_t symbol)
  130. {
  131.     unsigned hash = sym_hash(symbol);
  132.     int index = hash % table->length;
  133.     struct bucket *bucket;
  134.  
  135.     for (bucket = table->table[index]; bucket != NULL; bucket = bucket->next)
  136.     if (bucket->symbol == symbol)
  137.         return bucket->datum;
  138.     return NULL;
  139. }
  140.  
  141. static void rehash_table(struct table *table)
  142. {
  143.     int length = table->length;
  144.     int new_length = (length < 1024) ? (length << 1) : (length + 1024);
  145.     struct bucket **new_table
  146.     = (struct bucket **)malloc(sizeof(struct bucket *) * new_length);
  147.     int i;
  148.  
  149.     memset(new_table, 0, sizeof(struct bucket *) * new_length);
  150.  
  151.     for (i = 0; i < length; i++) {
  152.     struct bucket *bucket, *next;
  153.  
  154.     for (bucket = table->table[i]; bucket != NULL; bucket = next) {
  155.         int index = sym_hash(bucket->symbol) % new_length;
  156.  
  157.         next = bucket->next;
  158.         bucket->next = new_table[index];
  159.         new_table[index] = bucket;
  160.     }
  161.     }
  162.  
  163.     free(table->table);
  164.  
  165.     table->threshold = (new_length * 3) >> 1;
  166.     table->length = new_length;
  167.     table->table = new_table;
  168. }
  169.  
  170. static void table_add(struct table *table, obj_t symbol, void *datum)
  171. {
  172.     unsigned hash = sym_hash(symbol);
  173.     int index = hash % table->length;
  174.     struct bucket *bucket;
  175.  
  176.     for (bucket = table->table[index]; bucket != NULL; bucket = bucket->next)
  177.     if (bucket->symbol == symbol) {
  178.         bucket->datum = datum;
  179.         return;
  180.     }
  181.  
  182.     bucket = (struct bucket *)malloc(sizeof(struct bucket));
  183.     bucket->symbol = symbol;
  184.     bucket->datum = datum;
  185.     bucket->next = table->table[index];
  186.     table->table[index] = bucket;
  187.  
  188.     table->entries++;
  189.     if (table->entries >= table->threshold)
  190.     rehash_table(table);
  191. }
  192.  
  193. static void *table_remove(struct table *table, obj_t symbol)
  194. {
  195.     unsigned hash = sym_hash(symbol);
  196.     int index = hash % table->length;
  197.     struct bucket *bucket, **prev;
  198.  
  199.     for (prev = &table->table[index];
  200.      (bucket = *prev) != NULL;
  201.      prev = &bucket->next)
  202.     if (bucket->symbol == symbol) {
  203.         void *res = bucket->datum;
  204.         *prev = bucket->next;
  205.         free(bucket);
  206.         table->entries--;
  207.         return res;
  208.     }
  209.  
  210.     return NULL;
  211. }
  212.  
  213.  
  214. /* Utilities */
  215.  
  216. static void vmake_entry(struct table *table, obj_t name, void *datum,
  217.                boolean exported, char *template, va_list ap1, va_list ap2)
  218. {
  219.     char *ptr;
  220.     int len;
  221.     char *origin;
  222.     struct entry *old_entry = table_lookup(table, name);
  223.     struct entry *entry;
  224.  
  225.     if (old_entry && old_entry->datum == datum)
  226.     return;
  227.  
  228.     entry = (struct entry *)malloc(sizeof(struct entry));
  229.  
  230.     entry->name = name;
  231.     entry->datum = datum;
  232.     entry->exported = exported;
  233.  
  234.     len = strlen(template);
  235.     for (ptr = template; *ptr != '\0'; ptr++) {
  236.     if (*ptr == '%') {
  237.         len--;
  238.         switch (*++ptr) {
  239.           case '%':
  240.         break;
  241.           case 's':
  242.         len += strlen(sym_name(va_arg(ap1, obj_t)));
  243.         break;
  244.           default:
  245.         lose("Bogus thing in origin template: %%%c", *ptr);
  246.         }
  247.     }
  248.     }
  249.  
  250.     entry->origin = alloc_byte_string(len);
  251.     origin = (char *)string_chars(entry->origin);
  252.  
  253.     for(ptr = template; *ptr != '\0'; ptr++) {
  254.     if (*ptr == '%') {
  255.         switch (*++ptr) {
  256.           case '%':
  257.         *origin++ = '%';
  258.         break;
  259.           case 's':
  260.         {
  261.             char *name = sym_name(va_arg(ap2, obj_t));
  262.             while (*name != '\0')
  263.             *origin++ = *name++;
  264.         }
  265.         break;
  266.         }
  267.     }
  268.     else
  269.         *origin++ = *ptr;
  270.     }
  271.     *origin = '\0';
  272.  
  273.     if (old_entry)
  274.     error("%s clashes with %s", entry->origin, old_entry->origin);
  275.  
  276.     table_add(table, name, entry);
  277. }
  278. #if _USING_PROTOTYPES_
  279. static void make_entry(struct table *table, obj_t name, void *datum,
  280.                boolean exported, char *template, ...)
  281. {
  282.     va_list ap1, ap2;
  283.     va_start(ap1, template);
  284.     va_start(ap2, template);
  285.     vmake_entry(table, name, datum, exported, template, ap1, ap2);
  286.     va_end(ap1);
  287.     va_end(ap2);
  288. }
  289. #else
  290. static void make_entry(va_alist) va_dcl
  291. {
  292.     va_list ap1, ap2;
  293.     struct table *table;
  294.     obj_t name;
  295.     void *datum;
  296.     boolean exported;
  297.     char *template;
  298.     va_start(ap1);
  299.     va_start(ap2);
  300.     table = va_arg(ap1, struct table *);
  301.     table = va_arg(ap2, struct table *);
  302.     name = va_arg(ap1, obj_t);
  303.     name = va_arg(ap2, obj_t);
  304.     datum = va_arg(ap1, void *);
  305.     datum = va_arg(ap2, void *);
  306.     exported = va_arg(ap1, boolean);
  307.     exported = va_arg(ap2, boolean);
  308.     template = va_arg(ap1, char *);
  309.     template = va_arg(ap2, char *);
  310.     vmake_entry(table, name, datum, exported, template, ap1, ap2);
  311.     va_end(ap1);
  312.     va_end(ap2);
  313. }
  314. #endif
  315.  
  316. static obj_t prepend_prefix(obj_t name, struct use *use)
  317. {
  318.     char *prefix;
  319.     char *local_name;
  320.     obj_t res;
  321.  
  322.     if (use->prefix == obj_False)
  323.     return name;
  324.  
  325.     prefix = (char *)obj_ptr(struct string *, use->prefix)->chars;
  326.     local_name = (char *)malloc(strlen(prefix) + strlen(sym_name(name)) + 1);
  327.     strcpy(local_name, prefix);
  328.     strcat(local_name, sym_name(name));
  329.     res = symbol(local_name);
  330.     free(local_name);
  331.  
  332.     return res;
  333. }
  334.  
  335. static obj_t prefix_or_rename(obj_t name, struct use *use)
  336. {
  337.     obj_t ptr;
  338.  
  339.     for (ptr = use->rename; ptr != obj_Nil; ptr = TAIL(ptr))
  340.     if (name == HEAD(HEAD(ptr)))
  341.         return TAIL(HEAD(ptr));
  342.     return prepend_prefix(name, use);
  343. }
  344.  
  345. static boolean exported(obj_t name, struct use *use)
  346. {
  347.     return use->export == obj_True || memq(name, use->export);
  348. }
  349.  
  350. static void add_use(struct defn *defn, obj_t name)
  351. {
  352.     struct use *use = malloc(sizeof(struct use));    
  353.  
  354.     use->name = name;
  355.     use->import = obj_True;
  356.     use->prefix = obj_False;
  357.     use->exclude = obj_Nil;
  358.     use->rename = obj_Nil;
  359.     use->export = obj_Nil;
  360.     use->next = defn->use;
  361.  
  362.     defn->use = use;
  363. }
  364.  
  365.  
  366. /* Libraries. */
  367.  
  368. static struct library *make_library(obj_t name)
  369. {
  370.     struct library *library = malloc(sizeof(struct library));
  371.  
  372.     library->name = name;
  373.     library->defn = NULL;
  374.     library->modules = make_table();
  375.     library->completed = FALSE;
  376.     library->loading = FALSE;
  377.     library->busy = FALSE;
  378.     library->next = Libraries;
  379.     Libraries = library;
  380.  
  381.     table_add(LibraryTable, name, library);
  382.  
  383.     return library;
  384. }
  385.  
  386. struct library *find_library(obj_t name, boolean createp)
  387. {
  388.     struct library *library = table_lookup(LibraryTable, name);
  389.  
  390.     if (library == NULL && createp) {
  391.     struct defn *defn = malloc(sizeof(struct defn));
  392.     obj_t dylan_user = symbol("Dylan-User");
  393.     struct module *module;
  394.  
  395.     library = make_library(name);
  396.     module = make_module(dylan_user, library);
  397.  
  398.     module->defn = defn;
  399.  
  400.     defn->name = dylan_user;
  401.     defn->use = NULL;
  402.     defn->exports = obj_Nil;
  403.     defn->creates = obj_Nil;
  404.  
  405.     add_use(defn, symbol("Introspection"));
  406.     add_use(defn, symbol("Threads"));
  407.     add_use(defn, symbol("File-Descriptors"));
  408.     add_use(defn, symbol("Cheap-IO"));
  409.     add_use(defn, symbol("System"));
  410.     add_use(defn, symbol("Extensions"));
  411.     add_use(defn, symbol("Dylan"));
  412.  
  413.     make_entry(library->modules, module->name, module, FALSE,
  414.            "module %s implicitly defined in library %s",
  415.            module->name, library->name);
  416.     }
  417.  
  418.     return library;
  419. }
  420.  
  421. void define_library(struct defn *defn)
  422. {
  423.     struct library *library = find_library(defn->name, TRUE);
  424.  
  425.     if (library->defn)
  426.     error("Library %s multiply defined.\n", defn->name);
  427.  
  428.     library->defn = defn;
  429. }
  430.  
  431. static void complete_library(struct library *library)
  432. {
  433.     obj_t ptr;
  434.     struct defn *defn;
  435.     struct use *use;
  436.  
  437.     defn = library->defn;
  438.     if (defn == NULL) {
  439.     if (library->loading)
  440.         error("needed the definition of library %s before the "
  441.           "define library was found",
  442.           library->name);
  443.     library->loading = TRUE;
  444.     load_library(library->name);
  445.     if (library->defn == NULL)
  446.         error("loaded library %s, but never found the define library.\n",
  447.           library->name);
  448.     library->loading = FALSE;
  449.     return;
  450.     }
  451.  
  452.     if (library->busy)
  453.     error("Library %s circularly defined.\n", library->name);
  454.     library->busy = TRUE;
  455.  
  456.     for (ptr = defn->exports; ptr != obj_Nil; ptr = TAIL(ptr)) {
  457.     obj_t name = HEAD(ptr);
  458.     struct module *module = make_module(name, library);
  459.  
  460.     make_entry(library->modules, name, module, TRUE,
  461.            "module %s defined in library %s",
  462.            name, library->name);
  463.     }
  464.  
  465.     for (use = library->defn->use; use != NULL; use = use->next) {
  466.     obj_t use_name = use->name;
  467.     struct library *used_library = find_library(use_name, TRUE);
  468.     obj_t imports = obj_Nil;
  469.  
  470.     if (!used_library->completed)
  471.         complete_library(used_library);
  472.  
  473.     if (use->import == obj_True) {
  474.         struct table *modules = used_library->modules;
  475.         int i;
  476.         for (i = 0; i < modules->length; i++) {
  477.         struct bucket *bucket;
  478.         for (bucket = modules->table[i];
  479.              bucket != NULL;
  480.              bucket = bucket->next) {
  481.             struct entry *entry = (struct entry *)bucket->datum;
  482.             obj_t name = entry->name;
  483.             if (entry->exported && !memq(name, use->exclude)) {
  484.             obj_t local_name = prefix_or_rename(name, use);
  485.             make_entry(library->modules, local_name, entry->datum,
  486.                    exported(local_name, use),
  487.                    name == local_name
  488.                    ? "module %s imported from library %s"
  489.                    :"module %s imported from library %s as %s",
  490.                    name, use_name, local_name);
  491.             imports = pair(local_name, imports);
  492.             }
  493.         }
  494.         }
  495.     }
  496.     else {
  497.         for (ptr = use->import; ptr != obj_Nil; ptr = TAIL(ptr)) {
  498.         obj_t name = HEAD(ptr);
  499.         obj_t local_name = prepend_prefix(name, use);
  500.         struct entry *entry
  501.             = table_lookup(used_library->modules, name);
  502.         if (!entry || !entry->exported)
  503.             error("library %s can't import module %s from library %s "
  504.               "because it isn't exported.\n",
  505.               library->name, name, use_name);
  506.         make_entry(library->modules, local_name, entry->datum,
  507.                exported(local_name, use),
  508.                name == local_name
  509.                 ? "module %s imported from library %s"
  510.                 : "module %s imported from library %s as %s",
  511.                name, use_name, local_name);
  512.         imports = pair(local_name, imports);
  513.         }
  514.         for (ptr = use->rename; ptr != obj_Nil; ptr = TAIL(ptr)) {
  515.         obj_t rename = HEAD(ptr);
  516.         obj_t name = HEAD(rename);
  517.         obj_t local_name = TAIL(rename);
  518.         struct entry *entry
  519.             = table_lookup(used_library->modules, name);
  520.         if (!entry || !entry->exported)
  521.             error("library %s can't import module %s from library %s"
  522.               "because it isn't exported.\n",
  523.               library->name, name, use_name);
  524.         make_entry(library->modules, local_name, entry->datum,
  525.                exported(local_name, use),
  526.                name == local_name
  527.                 ? "module %s imported from library %s"
  528.                 : "module %s imported from library %s as %s",
  529.                name, use_name, local_name);
  530.         imports = pair(local_name, imports);
  531.         }
  532.     }
  533.     if (use->export != obj_True)
  534.         for (ptr = use->export; ptr != obj_Nil; ptr = TAIL(ptr))
  535.         if (!memq(HEAD(ptr), imports))
  536.             error("library %s can't re-export module %s because it "
  537.               "doesn't import it in the first place",
  538.               library->name, HEAD(ptr));
  539.     }
  540.  
  541.     library->busy = FALSE;
  542.     library->completed = TRUE;
  543. }
  544.  
  545.  
  546. /* Modules */
  547.  
  548. static struct module *make_module(obj_t name, struct library *home)
  549. {
  550.     struct module *module = malloc(sizeof(struct module));
  551.  
  552.     module->name = name;
  553.     module->home = home;
  554.     module->defn = NULL;
  555.     module->variables = make_table();
  556.     module->busy = FALSE;
  557.     module->completed = FALSE;
  558.     module->next = Modules;
  559.     Modules = module;
  560.  
  561.     return module;
  562. }
  563.  
  564. struct module *find_module(struct library *library, obj_t name,
  565.                boolean lose_if_not_there, boolean lose_if_imported)
  566. {
  567.     struct entry *entry;
  568.     struct module *module;
  569.  
  570.     if ((entry = table_lookup(library->modules, name)) == NULL) {
  571.     if (!lose_if_not_there)
  572.         return NULL;
  573.  
  574.     if (!library->completed) {
  575.         complete_library(library);
  576.         if ((entry = table_lookup(library->modules, name)) == NULL)
  577.         error("Unknown module %s in library %s",
  578.               name, library->name);
  579.     }
  580.     else
  581.         error("Unknown module %s in library %s", name, library->name);
  582.     }
  583.  
  584.     module = entry->datum;
  585.  
  586.     if (lose_if_imported && module->home != library)
  587.     error("Can't add code to module %s in library %s because it "
  588.           "is imported from library %s",
  589.           module->name,
  590.           library->name,
  591.           module->home->name);
  592.  
  593.     return module;
  594. }
  595.  
  596. void define_module(struct library *library, struct defn *defn)
  597. {
  598.     struct entry *entry;
  599.     struct module *module;
  600.  
  601.     entry = table_lookup(library->modules, defn->name);
  602.     if (entry == NULL && !library->completed) {
  603.     complete_library(library);
  604.     entry = table_lookup(library->modules, defn->name);
  605.     }
  606.     if (entry == NULL) {
  607.     module = make_module(defn->name, library);
  608.     make_entry(library->modules, defn->name, module, FALSE,
  609.            "module %s internal to library %s",
  610.            defn->name, library->name);
  611.     }
  612.     else {
  613.     module = entry->datum;
  614.  
  615.     if (module->home != library)
  616.         error("Can't define %s in library %s because its home "
  617.             "is library %s.\n",
  618.           entry->origin, library->name, module->home->name);
  619.  
  620.     if (module->defn)
  621.         error("Module %s multiply defined.\n", defn->name);
  622.     }
  623.  
  624.     module->defn = defn;
  625. }
  626.  
  627. static void
  628.     make_and_export_var(struct module *module, obj_t name, boolean created)
  629. {
  630.     struct entry *entry;
  631.     struct var *var;
  632.  
  633.     entry = table_lookup(module->variables, name);
  634.     if (entry) {
  635.     var = entry->datum;
  636.     if (var->variable.home == module) {
  637.         table_remove(module->variables, name);
  638.         free(entry);
  639.     }
  640.     else
  641.         var = make_var(name, module, var_Assumed);
  642.     }
  643.     else
  644.     var = make_var(name, module, var_Assumed);
  645.  
  646.     make_entry(module->variables, name, var, TRUE,
  647.            created
  648.            ? "variable %s created in module %s"
  649.            : "variable %s defined in module %s",
  650.            name, module->name);
  651.  
  652.     var->created = created;
  653. }
  654.  
  655. static void complete_module(struct module *module)
  656. {
  657.     obj_t ptr;
  658.     struct defn *defn;
  659.     struct use *use;
  660.  
  661.     if (module->busy)
  662.     error("Module %s circularly defined.", module->name);
  663.     module->busy = TRUE;
  664.  
  665.     defn = module->defn;
  666.     if (defn == NULL)
  667.     error("Attempt to use module %s before it is defined.", module->name);
  668.  
  669.     for (ptr = defn->exports; ptr != obj_Nil; ptr = TAIL(ptr))
  670.     make_and_export_var(module, HEAD(ptr), FALSE);
  671.     for (ptr = defn->creates; ptr != obj_Nil; ptr = TAIL(ptr))
  672.     make_and_export_var(module, HEAD(ptr), TRUE);
  673.  
  674.     for (use = module->defn->use; use != NULL; use = use->next) {
  675.     obj_t use_name = use->name;
  676.     struct module *used_module;
  677.     obj_t imports = obj_Nil;
  678.  
  679.     if (module->name == symbol("Dylan-User"))
  680.         used_module = find_module(library_Dylan, use_name, TRUE, FALSE);
  681.     else
  682.         used_module = find_module(module->home, use_name, TRUE, FALSE);
  683.  
  684.     if (!used_module->completed)
  685.         complete_module(used_module);
  686.  
  687.     if (use->import == obj_True) {
  688.         struct table *modules = used_module->variables;
  689.         int i;
  690.         for (i = 0; i < modules->length; i++) {
  691.         struct bucket *bucket;
  692.         for (bucket = modules->table[i];
  693.              bucket != NULL;
  694.              bucket = bucket->next) {
  695.             struct entry *entry = (struct entry *)bucket->datum;
  696.             obj_t name = entry->name;
  697.             if (entry->exported && !memq(name, use->exclude)) {
  698.             obj_t local_name = prefix_or_rename(name, use);
  699.             make_entry(module->variables, local_name, entry->datum,
  700.                    exported(local_name, use),
  701.                    name == local_name
  702.                    ? "variable %s imported from module %s"
  703.                    : "variable %s imported from "
  704.                      "module %s as %s",
  705.                    name, use_name, local_name);
  706.             imports = pair(local_name, imports);
  707.             }
  708.         }
  709.         }
  710.     }
  711.     else {
  712.         for (ptr = use->import; ptr != obj_Nil; ptr = TAIL(ptr)) {
  713.         obj_t name = HEAD(ptr);
  714.         obj_t local_name = prepend_prefix(name, use);
  715.         struct entry *entry
  716.             = table_lookup(used_module->variables, name);
  717.         if (!entry || !entry->exported)
  718.             error("module %s can't import variable %s from module %s "
  719.               "because it isn't exported.",
  720.               module->name, name, use_name);
  721.         make_entry(module->variables, local_name, entry->datum,
  722.                exported(local_name, use),
  723.                name == local_name
  724.                 ? "variable %s imported from module %s"
  725.                 : "variable %s imported from module %s as %s",
  726.                name, use_name, local_name);
  727.         imports = pair(local_name, imports);
  728.         }
  729.         for (ptr = use->rename; ptr != obj_Nil; ptr = TAIL(ptr)) {
  730.         obj_t rename = HEAD(ptr);
  731.         obj_t name = HEAD(rename);
  732.         obj_t local_name = TAIL(rename);
  733.         struct entry *entry
  734.             = table_lookup(used_module->variables, name);
  735.         if (!entry || !entry->exported)
  736.             error("module %s can't import variable %s from module %s"
  737.               "because it isn't exported.",
  738.               module->name, name, use_name);
  739.         make_entry(module->variables, local_name, entry->datum,
  740.                exported(local_name, use),
  741.                name == local_name
  742.                 ? "variable %s imported from module %s"
  743.                 : "variable %s imported from module %s as %s",
  744.                name, use_name, local_name);
  745.         imports = pair(local_name, imports);
  746.         }
  747.     }
  748.     if (use->export != obj_True)
  749.         for (ptr = use->export; ptr != obj_Nil; ptr = TAIL(ptr))
  750.         if (!memq(HEAD(ptr), imports))
  751.             error("module %s can't re-export variable %s because it "
  752.               "doesn't import it in the first place",
  753.               module->name, HEAD(ptr));
  754.     }
  755.  
  756.     module->busy = FALSE;
  757.     module->completed = TRUE;
  758. }
  759.  
  760.  
  761. /* Variables. */
  762.  
  763. static struct var *make_var(obj_t name, struct module *home,enum var_kind kind)
  764. {
  765.     struct var *var = malloc(sizeof(struct var));
  766.  
  767.     var->created = FALSE;
  768.     var->next = Vars;
  769.     Vars = var;
  770.  
  771.     var->variable.name = name;
  772.     var->variable.home = home;
  773.     var->variable.defined = FALSE;
  774.     var->variable.kind = kind;
  775.     var->variable.value = obj_Unbound;
  776.     var->variable.type = obj_False;
  777.     var->variable.function = func_Maybe;
  778.     var->variable.ref_file = obj_False;
  779.     var->variable.ref_line = 0;
  780.  
  781.     return var;
  782. }
  783.  
  784. struct var *find_var(struct module *module, obj_t name, boolean writeable,
  785.              boolean createp)
  786. {
  787.     struct entry *entry = table_lookup(module->variables, name);
  788.     struct var *var;
  789.  
  790.     if (entry == NULL) {
  791.     if (createp) {
  792.         if (!module->completed && !InitializingVars) {
  793.         complete_module(module);
  794.         entry = table_lookup(module->variables, name);
  795.         }
  796.     }
  797.     else
  798.         return NULL;
  799.     }
  800.     if (entry == NULL) {
  801.     var = make_var(name, module,
  802.                writeable ? var_AssumedWriteable : var_Assumed);
  803.     make_entry(module->variables, name, var, FALSE,
  804.            "variable %s internal to module %s",
  805.            name, module->name);
  806.     }
  807.     else {
  808.     var = entry->datum;
  809.     if (writeable) {
  810.         switch (var->variable.kind) {
  811.           case var_Assumed:
  812.         var->variable.kind = var_AssumedWriteable;
  813.         break;
  814.           case var_AssumedWriteable:
  815.           case var_Variable:
  816.         break;
  817.           default:
  818.         error("Constant %s in module %s library %s is not changeable.",
  819.               name, module->name, module->home->name);
  820.         }
  821.     }
  822.     }
  823.     return var;
  824. }
  825.  
  826. struct variable
  827.     *find_variable(struct module *module, obj_t name, boolean writeable,
  828.            boolean createp)
  829. {
  830.     struct var *var = find_var(module, name, writeable, createp);
  831.  
  832.     if (var)
  833.     return &var->variable;
  834.     else
  835.     return NULL;
  836. }
  837.  
  838. void define_variable(struct module *module, obj_t name, enum var_kind kind)
  839. {
  840.     struct var *var = find_var(module, name, kind == var_Variable, TRUE);
  841.  
  842.     switch (var->variable.kind) {
  843.       case var_Assumed:
  844.     if (kind != var_Method)
  845.         var->variable.kind = kind;
  846.     break;
  847.       case var_AssumedWriteable:
  848.     if (kind == var_Variable)
  849.         var->variable.kind = var_Variable;
  850.     else if (kind != var_Method)
  851.         error("Constant %s in module %s library %s is not changeable.",
  852.           name, module->name, module->home->name);
  853.     break;
  854.       default:
  855.     if (kind != var_Method)
  856.         error("variable %s in module %s multiply defined.",
  857.           name, module->name);
  858.     break;
  859.     }
  860.  
  861.     if (var->created) {
  862.     if (var->variable.home == module)
  863.         error("variable %s must be defined by a user of module %s\n",
  864.           name, module->name);
  865.     else if (kind!=var_Method && var->variable.home->home!=module->home)
  866.         error("variable %s must be defined in library %s, not %s\n",
  867.           name, var->variable.home->home->name,
  868.           module->home->name);
  869.     }
  870.     else {
  871.     if (kind != var_Method && var->variable.home != module)
  872.         error("variable %s must be defined in module %s, not %s\n",
  873.           name, var->variable.home->name, module->name);
  874.     }
  875.  
  876.     var->variable.defined = TRUE;
  877. }
  878.  
  879.  
  880. /* Debugger support. */
  881.  
  882. void list_libraries(void)
  883. {
  884.     struct library *library;
  885.  
  886.     for (library = Libraries; library != NULL; library = library->next) {
  887.     fputs(sym_name(library->name), stdout);
  888.     if (library->completed)
  889.         printf("\n");
  890.     else if (library->defn)
  891.         printf(" [defined, but not filled in.]\n");
  892.     else
  893.         printf(" [no definition]\n");
  894.     }
  895. }
  896.  
  897. obj_t library_name(struct library *library)
  898. {
  899.     return library->name;
  900. }
  901.  
  902. void list_modules(struct library *library)
  903. {
  904.     struct table *table = library->modules;
  905.     int i;
  906.     struct bucket *bucket;
  907.     
  908.     for (i = 0; i < table->length; i++) {
  909.     for (bucket = table->table[i]; bucket != NULL; bucket = bucket->next) {
  910.         struct entry *entry = bucket->datum;
  911.         struct module *module = entry->datum;
  912.  
  913.         printf("%c%c ",
  914.            entry->exported ? 'x' : ' ',
  915.            module->home == library ? ' ' : 'i');
  916.         fputs(sym_name(entry->name), stdout);
  917.         if (module->completed)
  918.         printf("\n");
  919.         else if (module->defn)
  920.         printf(" [defined, but not filled in.]\n");
  921.         else
  922.         printf(" [no definition]\n");
  923.     }
  924.     }
  925. }
  926.  
  927. obj_t module_name(struct module *module)
  928. {
  929.     return module->name;
  930. }
  931.  
  932.  
  933.  
  934. /* GC stuff. */
  935.  
  936. static int scav_unbound(struct object *ptr)
  937. {
  938.     return sizeof(struct object);
  939. }
  940.  
  941. static obj_t trans_unbound(obj_t unbound)
  942. {
  943.     return transport(unbound, sizeof(struct object));
  944. }
  945.  
  946. static void scav_use(struct use *use)
  947. {
  948.     scavenge(&use->name);
  949.     scavenge(&use->import);
  950.     scavenge(&use->prefix);
  951.     scavenge(&use->exclude);
  952.     scavenge(&use->rename);
  953.     scavenge(&use->export);
  954. }
  955.  
  956. static void scav_defn(struct defn *defn)
  957. {
  958.     struct use *use;
  959.  
  960.     scavenge(&defn->name);
  961.     scavenge(&defn->exports);
  962.     scavenge(&defn->creates);
  963.  
  964.     for (use = defn->use; use != NULL; use = use->next)
  965.     scav_use(use);
  966. }
  967.  
  968. static void scav_table(struct table *table, boolean of_entries)
  969. {
  970.     int i;
  971.     struct bucket *bucket;
  972.     
  973.     for (i = 0; i < table->length; i++) {
  974.     for (bucket = table->table[i]; bucket != NULL; bucket = bucket->next) {
  975.         scavenge(&bucket->symbol);
  976.         if (of_entries) {
  977.         struct entry *entry = bucket->datum;
  978.         scavenge(&entry->name);
  979.         scavenge(&entry->origin);
  980.         }
  981.     }
  982.     }
  983. }
  984.  
  985. static void scav_var(struct var *var)
  986. {
  987.     scavenge(&var->variable.name);
  988.     scavenge(&var->variable.value);
  989.     scavenge(&var->variable.type);
  990.     scavenge(&var->variable.ref_file);
  991. }
  992.  
  993. static void scav_module(struct module *module)
  994. {
  995.     scavenge(&module->name);
  996.     scav_defn(module->defn);
  997.     scav_table(module->variables, TRUE);
  998. }
  999.  
  1000. static void scav_library(struct library *library)
  1001. {
  1002.     scavenge(&library->name);
  1003.     scav_defn(library->defn);
  1004.     scav_table(library->modules, TRUE);
  1005. }
  1006.  
  1007. void scavenge_module_roots(void)
  1008. {
  1009.     struct library *library;
  1010.     struct module *module;
  1011.     struct var *var;
  1012.  
  1013.     scav_table(LibraryTable, FALSE);
  1014.     for (library = Libraries; library != NULL; library = library->next)
  1015.     scav_library(library);
  1016.     for (module = Modules; module != NULL; module = module->next)
  1017.     scav_module(module);
  1018.     for (var = Vars; var != NULL; var = var->next)
  1019.     scav_var(var);
  1020.     scavenge(&obj_Unbound);
  1021.     scavenge(&obj_UnboundClass);
  1022. }
  1023.  
  1024.  
  1025. /* Initialization stuff. */
  1026.  
  1027. void make_module_classes(void)
  1028. {
  1029.     obj_UnboundClass = make_builtin_class(scav_unbound, trans_unbound);
  1030. }
  1031.  
  1032. void init_modules(void)
  1033. {
  1034.     obj_t dylan = symbol("Dylan");
  1035.     obj_t stuff = symbol("Builtin-Stuff");
  1036.  
  1037.     LibraryTable = make_table();
  1038.  
  1039.     library_Dylan = find_library(dylan, TRUE);
  1040.  
  1041.     {
  1042.     /* Define the dylan-user library. */
  1043.     struct defn *defn = malloc(sizeof(*defn));
  1044.     struct use *use = malloc(sizeof(*use));
  1045.     defn->name = symbol("Dylan-User");
  1046.     defn->use = NULL;
  1047.     defn->exports = obj_Nil;
  1048.     defn->creates = NULL;
  1049.     add_use(defn, dylan);
  1050.     define_library(defn);
  1051.     }
  1052.     
  1053.     module_BuiltinStuff = make_module(stuff, library_Dylan);
  1054.     make_entry(library_Dylan->modules, stuff, module_BuiltinStuff, FALSE,
  1055.            "module %s internal to library %s", stuff, dylan);
  1056.  
  1057.     obj_Unbound = alloc(obj_UnboundClass, sizeof(struct object));
  1058. }
  1059.  
  1060. void init_module_classes(void)
  1061. {
  1062.     init_builtin_class(obj_UnboundClass, "<unbound-marker>",
  1063.                obj_ObjectClass, NULL);
  1064. }
  1065.  
  1066. void done_initializing_vars(void)
  1067. {
  1068.     InitializingVars = FALSE;
  1069. }
  1070.  
  1071. void finalize_modules(void)
  1072. {
  1073.     struct library *library;
  1074.     struct module *module;
  1075.     struct var *var;
  1076.     boolean warning_printed = FALSE;
  1077.  
  1078.     for (library = Libraries; library != NULL; library = library->next)
  1079.     if (!library->completed)
  1080.         complete_library(library);
  1081.     for (module = Modules; module != NULL; module = module->next)
  1082.     if (!module->completed)
  1083.         complete_module(module);
  1084.  
  1085.     for (var = Vars; var != NULL; var = var->next) {
  1086.     if (var->variable.defined) {
  1087.         switch (var->variable.kind) {
  1088.           case var_Assumed:
  1089.         var->variable.kind = var_GenericFunction;
  1090.         break;
  1091.  
  1092.           case var_AssumedWriteable:
  1093.         error("Constant %s in module %s library %s is not changeable.",
  1094.               var->variable.name, var->variable.home->name,
  1095.               var->variable.home->home->name);
  1096.         break;
  1097.  
  1098.           default:
  1099.         break;
  1100.         }
  1101.     }
  1102.     }
  1103.  
  1104.     for (library = Libraries; library != NULL; library = library->next) {
  1105.     boolean library_printed = FALSE;
  1106.     for (module = Modules; module != NULL; module = module->next) {
  1107.         if (module->home == library) {
  1108.         boolean module_printed = FALSE;
  1109.         for (var = Vars; var != NULL; var = var->next) {
  1110.             if (var->variable.home==module && !var->variable.defined) {
  1111.             if (!warning_printed) {
  1112.                 fprintf(stderr, "Warning: the following variables "
  1113.                     "are undefined:\n");
  1114.                 warning_printed = TRUE;
  1115.             }
  1116.             if (!library_printed) {
  1117.                 fprintf(stderr, "  in library %s:\n",
  1118.                     sym_name(library->name));
  1119.                 library_printed = TRUE;
  1120.             }
  1121.             if (!module_printed) {
  1122.                 fprintf(stderr, "    in module %s:\n",
  1123.                     sym_name(module->name));
  1124.                 module_printed = TRUE;
  1125.             }
  1126.             fprintf(stderr, "      %s",
  1127.                 sym_name(var->variable.name));
  1128.             if (var->variable.ref_file != obj_False) {
  1129.                 fprintf(stderr, " [%s, line %d]",
  1130.                     string_chars(var->variable.ref_file),
  1131.                     var->variable.ref_line);
  1132.             }
  1133.             fprintf(stderr, "\n");
  1134.             }
  1135.         }
  1136.         }
  1137.     }
  1138.     }
  1139. }
  1140.